<?php

namespace app\health\controller;

use app\common\library\time\TimeHelper;
use app\health\model\HealthEvaluationClick;
use app\health\model\HealthGoodsCategory;
use app\health\model\HealthUserChannel;
use app\health\model\HealthWebsiteBanner;
use think\Cache;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Log;

class Index extends ApiBase
{
    protected $noNeedLogin = ['index', 'evaluation', 'evaluation_click', 'popup', 'dev'];

    public function index()
    {
        //type=0 小程序内部路径 type=1外部url链接
        //page 路径或url链接
        $data           = [
            'nav' => [
                [
                    'name'   => '保健知识',
                    'icon'   => request()->domain() . '/assets/img/nav/baojian.png',
                    'tag'    => '',
                    'type'   => 0,
                    'params' => ['page' => 'pages/homeNav/bjzs']
                ],
                [
                    'name'   => '自诊测评',
                    'icon'   => request()->domain() . '/assets/img/nav/zizhen.png',
                    'tag'    => '',
                    'type'   => 0,
                    'params' => ['page' => 'pages/homeNav/zzcp']
                ],
                [
                    'name'   => '在线问诊',
                    'icon'   => request()->domain() . '/assets/img/nav/wenzhen.png',
                    'tag'    => '0元问诊',
                    'type'   => 0,
                    'params' => ['page' => 'pages/homeNav/zxwz']
                ],
                [
                    'name'   => '重疾二诊',
                    'icon'   => request()->domain() . '/assets/img/nav/zhongji.png',
                    'tag'    => '',
                    'type'   => 0,
                    'params' => ['page' => 'pages/homeNav/zjez']
                ],
                [
                    'name'   => '协助挂号',
                    'icon'   => request()->domain() . '/assets/img/nav/guahao.png',
                    'tag'    => '名医号源',
                    'type'   => 0,
                    'params' => ['page' => 'pages/homeNav/xzgh']
                ]
            ]
        ];
        $data['banner'] = HealthWebsiteBanner::where('position', 1)->where('deleted', 0)->where('status', 1)->field('id,name,image,type,params,status')->select();
        // $data['banner'] = [
        //     [
        //         'id'     => 1,
        //         'name'   => '关爱老人,定时体检',
        //         'image'  => request()->domain() . '/resource/image/banner/banner01.png',
        //         'type'   => 0,
        //         'params' => [
        //             'appid' => '',
        //             'page'  => '/pages/vote/index'
        //         ]
        //     ],
        //     [
        //         'id'     => 1,
        //         'name'   => '关爱老人,定时体检',
        //         'image'  => request()->domain() . '/resource/image/banner/banner02.png',
        //         'type'   => 0,
        //         'params' => [
        //             'appid' => '',
        //             'page'  => '/pages/vote/article'
        //         ]
        //     ],
        //     [
        //         'id'     => 1,
        //         'name'   => '千万神券',
        //         'image'  => request()->domain() . '/resource/image/banner/banner03.png',
        //         'type'   => 1,
        //         'params' => [
        //             'appid' => 'wxa918198f16869201',
        //             'page'  => '/pages/web/web?specialUrl=1&src=https%3A%2F%2Fmobile.yangkeduo.com%2Fduo_theme_activity.html%3F__page%3Dduo_tenmillion_coupon%26_pdd_fs%3D1%26pid%3D1671718_269262058%26customParameters%3D%257B%2522uid%2522%253A%2522baoyu%2522%257D%26cpsSign%3DTMC_230627_1671718_269262058_038d7a443e734b03f41e0f4b48e80c1a%26_x_ddjb_act%3D%257B%2522st%2522%253A%2522128%2522%257D%26duoduo_type%3D2'
        //         ]
        //     ]
        // ];
        $goodsCategory          = HealthGoodsCategory::where('is_enable', 1)->order('sort', 'desc')->field('id,name')->select();
        $data['goods_category'] = $goodsCategory;
        $this->success('success', $data);
    }

    /**
     * 测评数据列表页
     * @Author 张旭鹏
     * @DateTime 2023-06-21
     */
    public function evaluation()
    {
        $hot   = [
            [
                'name' => '压力评测',
                'icon' => cdnurl('/assets/img/evaluation/evaluation_04.png', true),
                'url'  => 'https://www.wenjuan.pub/s/eERJNv6/#',
                'code' => 'eERJNv6',
            ],
            [
                'name' => '抑郁评测',
                'icon' => cdnurl('/assets/img/evaluation/evaluation_03.png', true),
                'url'  => 'https://www.wenjuan.pub/s/BzMFbu/#',
                'code' => 'BzMFbu'
            ],
            [
                'name' => '心理评测',
                'icon' => cdnurl('/assets/img/evaluation/evaluation_02.png', true),
                'url'  => 'https://www.wenjuan.group/s/z2Mbuen/',
                'code' => 'z2Mbuen'
            ],
            [
                'name' => '亚健康评测',
                'icon' => cdnurl('/assets/img/evaluation/evaluation_01.png', true),
                'url'  => 'https://www.wenjuan.pub/s/ryU3Ej8/#',
                'code' => 'ryU3Ej8'
            ]
        ];
        $lists = [
            [
                'category_name' => '疾病风险评测',
                'lists'         => [
                    [
                        'name' => '颈椎健康测评',
                        'url'  => 'https://www.wenjuan.pub/s/IrAFJzk/#',
                        'desc' => '45w+人测过',
                        'code' => 'IrAFJzk'
                    ],
                    [
                        'name' => '糖尿病风险测试',
                        'url'  => 'https://www.wenjuan.pub/s/B7bM3er/#',
                        'desc' => '45w+人测过',
                        'code' => 'B7bM3er'
                    ],
                    [
                        'name' => '癌症体质自测',
                        'url'  => 'https://www.wenjuan.pub/s/QjquYrt/#',
                        'desc' => '45w+人测过',
                        'code' => 'QjquYrt'
                    ],
                    [
                        'name' => '阿尔茨海默病（老年痴呆症）在线测评',
                        'url'  => 'https://www.wenjuan.pub/s/NJbU7fh/#',
                        'desc' => '45w+人测过',
                        'code' => 'NJbU7fh'
                    ],
                    [
                        'name' => '亚健康状况自我检测',
                        'url'  => 'https://www.wenjuan.pub/s/ryU3Ej8/#',
                        'desc' => '45w+人测过',
                        'code' => 'ryU3Ej8'
                    ],
                    [
                        'name' => '卒中风险评估量表',
                        'url'  => 'https://www.wenjuan.pub/s/EjAnyeie/#',
                        'desc' => '45w+人测过',
                        'code' => 'EjAnyeie'
                    ],
                ]
            ],
            [
                'category_name' => '生活心理评测',
                'lists'         => [
                    [
                        'name' => '抑郁自评量表（SDS）',
                        'url'  => 'https://www.wenjuan.pub/s/BzMFbu/#',
                        'desc' => '45w+人测过',
                        'code' => 'BzMFbu'
                    ],
                    [
                        'name' => '教师职业倦怠量表',
                        'url'  => 'https://www.wenjuan.pub/s/vuIVZfV/#',
                        'desc' => '45w+人测过',
                        'code' => 'vuIVZfV'
                    ],
                    [
                        'name' => '想象力测试',
                        'url'  => 'https://www.wenjuan.pub/s/nEBvQnc/#',
                        'desc' => '45w+人测过',
                        'code' => 'nEBvQnc'
                    ],
                    [
                        'name' => '自律能力测试',
                        'url'  => 'https://www.wenjuan.pub/s/viyIZzA/#',
                        'desc' => '45w+人测过',
                        'code' => 'viyIZzA'
                    ],
                    [
                        'name' => 'UCLA孤独量表',
                        'url'  => 'https://www.wenjuan.pub/s/7VFjMr0/#',
                        'desc' => '45w+人测过',
                        'code' => '7VFjMr0'
                    ],
                    [
                        'name' => '菲尔人格魅力测试',
                        'url'  => 'https://www.wenjuan.pub/s/r2QzE3v/#',
                        'desc' => '45w+人测过',
                        'code' => 'r2QzE3v'
                    ],
                    [
                        'name' => '社会支持评定量表(SSRS)',
                        'url'  => 'https://www.wenjuan.pub/s/2ai6Vn/#',
                        'desc' => '45w+人测过',
                        'code' => '2ai6Vn'
                    ],
                    [
                        'name' => 'PSTRI压力测试',
                        'url'  => 'https://www.wenjuan.pub/s/eERJNv6/#',
                        'desc' => '45w+人测过',
                        'code' => 'eERJNv6'
                    ],
                    [
                        'name' => '失眠严重程度指数量表(ISI)',
                        'url'  => 'https://www.wenjuan.pub/s/7fQRVf/#',
                        'desc' => '45w+人测过',
                        'code' => '7fQRVf'
                    ],
                    [
                        'name' => 'DISC性格测试',
                        'url'  => 'https://www.wenjuan.pub/s/v2iYZnRr/#',
                        'desc' => '45w+人测过',
                        'code' => 'v2iYZnRr'
                    ],
                ]
            ]

        ];
        $this->success('', [
            'title'            => '一分钟评测',
            'sub_title'        => '轻松评估你的健康状况',
            'evaluation_hot'   => $hot,
            'evaluation_lists' => $lists
        ]);
    }

    /**
     * 测评点击统计
     */
    public function evaluation_click()
    {
        $code = $this->request->param('code');
        if ($code) {
            HealthEvaluationClick::create([
                'code'        => $code,
                'user_id'     => $this->auth->id ?: 0,
                'ip'          => $this->request->ip(),
                'create_time' => time()
            ]);
        }
        $this->success('success');
    }

    /**
     * 首页弹窗
     * @return void
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function popup(): void
    {
        $is_show = 0;
        $popup   = [];
        if ($this->auth) {
            $isFirstUser = TimeHelper::isToday($this->auth->created_at);//是否新用户/首次登录
            $isPopup     = Cache::get('index:popup');
            $is_show     = ($isFirstUser && $isPopup === false) ? 1 : 0;
            Cache::set('index:popup', 1, TimeHelper::secondEndToday());
            $userChannel    = HealthUserChannel::where('user_id', $this->auth->id)->find();
            $popup['style'] = in_array($userChannel['pid'], [2, 24, 25]) ? 1 : 2;//弹框样式1=健身用户,2=其它用户
        }
        $this->backTodata(compact('is_show', 'popup'));
    }

    public function dev()
    {
    }
}
